﻿using System;
using System.Collections;
using System.Reflection;

namespace ConnectLib.DAO
{
    public class DeliveryGroupDao
    {
        private string _delId;
        private string _invIds;
        private DateTime _locationDate;
        private string _locationAt;

        public string DelId
        {
            get { return _delId; }
            set { _delId = value; }
        }

        public string InvIds
        {
            get { return _invIds; }
            set { _invIds = value; }
        }

        public DateTime LocationDate
        {
            get { return _locationDate; }
            set { _locationDate = value; }
        }

        public string LocationAt
        {
            get { return _locationAt; }
            set { _locationAt = value; }
        }

        public void InitEmptyValue()
        {
            _delId = string.Empty;
            _invIds = string.Empty;
            _locationDate = DateTime.Now;
            _locationAt = string.Empty;
        }

        public DeliveryGroupDao()
        {
            InitEmptyValue();
        }

        public DeliveryGroupDao(Hashtable hsObj)
        {
            InitEmptyValue();
            try
            {
                PropertyInfo[] arrProInfo = this.GetType().GetProperties();
                PropertyInfo proInfo;
                object objValue;
                string sValue;
                string proName;
                int i = 0;

                for (; i < arrProInfo.Length; i++)
                {
                    proInfo = arrProInfo[i];
                    proName = proInfo.Name.ToLower();
                    if (hsObj.ContainsKey(proName))
                    {
                        objValue = hsObj[proName];
                        if (proInfo.PropertyType.Name.ToLower() == "byte[]")
                        {
                            if (objValue != null && objValue != DBNull.Value)
                            {
                                proInfo.SetValue(this, (byte[]) objValue, null);
                            }
                        }
                        else
                        {
                            sValue = Convert.ToString(objValue);
                            if (string.IsNullOrEmpty(sValue))
                                continue;
                            switch (proInfo.PropertyType.Name.ToLower())
                            {
                                case "byte":
                                    proInfo.SetValue(this, Convert.ToByte(objValue), null);
                                    break;
                                case "int64":
                                    proInfo.SetValue(this, Convert.ToInt64(objValue), null);
                                    break;
                                case "int32":
                                    proInfo.SetValue(this, Convert.ToInt32(objValue), null);
                                    break;
                                case "datetime":
                                    proInfo.SetValue(this, Convert.ToDateTime(objValue), null);
                                    break;
                                case "byte[]":
                                    break;
                                default:
                                    proInfo.SetValue(this, objValue, null);
                                    break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
